/*
 * Created by: vadikv
 * Created: Sunday, April 06, 2008
 */

using System;
using System.Collections.Generic;
using OneDayModel.Interfaces;
using OneDayModel.Model;
using OneDayModel.PresentationLogic;
using OneDayModel.Tests.Mocks;

namespace OneDayModel.Tests.Fits.UIUsers
{
    public class ExisitngUserProfileUser : IExistingUserProfileUI
    {
        private IEnumerable<string> roles;
        private IEnumerable<KeyValuePair<string, string>> attributes;
        private bool readOnly;
        private IDictionary<string, IRoleAttribute> attributeDefinitons;
        private bool? chatEnabled = null;
        private SessionContext sessionContext = new SessionContext();
        private MockRepository repository = new MockRepository();

        public bool? ChatEnabled
        {
            get { return chatEnabled; }
        }

        public void OpenUserProfile(string userAlias)
        {
            new ExistingUserProfileUILogic(this, new ExistingUserProfileModel(repository));
            onShowExistingUserProfile(this, new ShowExistingUserProfileArgs(userAlias, sessionContext));
        }


        public event EventHandler<ShowExistingUserProfileArgs> onShowExistingUserProfile;

        public bool EnableChat
        {
            get { return chatEnabled.Value; }
            set { chatEnabled = value; }
        }

        #region IExistingUserProfileUI Members

        public void ShowRoles(IEnumerable<string> userRoles)
        {
            roles = userRoles;
        }

        public void ShowAttributes(IEnumerable<KeyValuePair<string, string>> attributes, bool readOnly)
        {
            this.attributes = attributes;
            this.readOnly = readOnly;
        }

        public void SetAttributesDefinitions(IDictionary<string, IRoleAttribute> allAttributesDefinitions)
        {
            attributeDefinitons = allAttributesDefinitions;
        }

        #endregion

        public string GetShownAttributes()
        {
            string separator = "";
            string result = "";
            foreach (KeyValuePair<string, string> record in attributes)
            {
                result += separator + string.Format("{0}={1}", attributeDefinitons[record.Key].DisplayName, record.Value);
                separator = ";";
            }
            return result;
        }

        public string GetShownRoles()
        {
            string separator = "";
            string result = "";
            foreach (string role in roles)
            {
                result += separator + role;
                separator = ";";
            }
            return result;
        }

        public string GetEditableAttributes()
        {
            if (readOnly)
                return "";
            return "not expected";
        }

        public void SetLoggedInUser(string userAlias)
        {
            if (userAlias == null)
                sessionContext.User = null;
            else
                sessionContext.User = repository.GetUserByAlias(userAlias);
        }
    }
}